﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace CreaMotion.Forms
{
	public partial class RelationshipDesignForm : Window 
    {
        
        #region WindowResult

        public DialogResult WindowResult = DialogResult.Close;

        public enum DialogResult
        {
            Save,
            Close
        }

        #endregion

        #region Relationships

        ObservableCollection<TempRelationship> Relationships = new ObservableCollection<TempRelationship>();
        public ObservableCollection<TempRelationship> relationships
        {
            get
            {
                return Relationships;
            }
        }

        #endregion

        #region RelationshipSource

        public List<Interfaces.Relationship> RelationshipSource { get; set; }

        #endregion

        #region FirstTableFields

        ObservableCollection<UserControls.FieldItem> FirstTableFields = new ObservableCollection<UserControls.FieldItem>();
        public ObservableCollection<UserControls.FieldItem> firstTableFields
        {
            get
            {
                return FirstTableFields;
            }
        }

        #endregion

        #region SecoundTableFields

        ObservableCollection<UserControls.FieldItem> SecoundTableFields = new ObservableCollection<UserControls.FieldItem>();
        public ObservableCollection<UserControls.FieldItem> secoundTableFields
        {
            get
            {
                return SecoundTableFields;
            }
        }

        #endregion

        #region Tables

        #region FirstTable

        Interfaces.Table firstTable;
        public Interfaces.Table FirstTable
        {
            get { return firstTable; }
            set { firstTable = value; }
        }

        #endregion

        #region SecoundTable

        Interfaces.Table secoundTable;
        public Interfaces.Table SecoundTable
        {
            get { return secoundTable; }
            set { secoundTable = value; }
        }

        #endregion

        #endregion

        public RelationshipDesignForm()
		{
			this.InitializeComponent();

            cmbFirstTable.SelectionChanged += new SelectionChangedEventHandler(cmbFirstTable_SelectionChanged);
            cmbSecoundTable.SelectionChanged += new SelectionChangedEventHandler(cmbSecoundTable_SelectionChanged);

			btAddRelationship.Click += new RoutedEventHandler(btAddRelationship_Click);
            btClearRelationship.Click += new RoutedEventHandler(btClearRelationship_Click);
            btSaveRelationship.Click += new RoutedEventHandler(btSaveRelationship_Click);
            btCloseForm.Click += new RoutedEventHandler(btCloseForm_Click);
        }

        #region ShowDialog

        public virtual DialogResult ShowDialog()
        {
            base.ShowDialog();
            return WindowResult;
        }
        
        #endregion

        #region CloseForm

        void btCloseForm_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult DR = MessageBox.Show("Do you want to quit ?","Relationship Design Form",MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (DR != MessageBoxResult.Yes) return;

            WindowResult = DialogResult.Close;

            this.Close();
        }

        #endregion

        #region SaveForm

        void btSaveRelationship_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult DR = MessageBox.Show("Do you want to save ?", "Save Relationships", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (DR != MessageBoxResult.Yes) return;

            WindowResult = DialogResult.Save;

            this.Close();
        }

        #endregion

        #region Clear Relationships

        void btClearRelationship_Click(object sender, RoutedEventArgs e)
        {
            ListRelationships.Items.Clear();
        }

        #endregion

        #region Add RelationShip

        void btAddRelationship_Click(object sender, RoutedEventArgs e)
        {
            Relationships.Add(new TempRelationship());
        }
        
        #endregion

        #region Select Tables

        void cmbSecoundTable_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            String TableName = (cmbSecoundTable.SelectedValue as UserControls.TableItem).Source.TableName;

            Relationships.Clear();
            SecoundTableFields.Clear();

            SecoundTable = tableSource.Find(delegate(Interfaces.Table tbl) { return tbl.TableName == TableName; });

            foreach (Interfaces.Field field in (from tmp in tableSource
                                                where tmp.TableName == TableName
                                                select tmp.Fields).FirstOrDefault())
            {
                SecoundTableFields.Add(new UserControls.FieldItem() { Source = field });
            }

            if (RelationshipSource == null) return;

            foreach (Interfaces.Relationship Relationship in (from tmp in RelationshipSource
                                                              where tmp.FirstTableName == FirstTable.TableName && tmp.SecoundTableName == SecoundTable.TableName
                                                              select tmp).ToList())
            {
                TempRelationship tmp = new TempRelationship();

                tmp.FirstField = new UserControls.FieldItem() { Source = Relationship.FirstField };
                tmp.FirstFieldType = Relationship.FirstField.RelationshipType;

                tmp.SecoundField = new UserControls.FieldItem() { Source = Relationship.SecoundField };
                tmp.SecoundFieldType = Relationship.SecoundField.RelationshipType;
                
                Relationships.Add(tmp);
            }
        }

        void cmbFirstTable_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            String TableName = (cmbFirstTable.SelectedValue as UserControls.TableItem).Source.TableName;

            Relationships.Clear();
            FirstTableFields.Clear();

            FirstTable = tableSource.Find(delegate(Interfaces.Table tbl){ return tbl.TableName == TableName; });

            foreach (Interfaces.Field field in (from tmp in tableSource
                                                where tmp.TableName == TableName
                                                select tmp.Fields).FirstOrDefault())
            {
                FirstTableFields.Add(new UserControls.FieldItem() { Source = field });
            }
        }

        #endregion

        #region SelectTableSource
        ObservableCollection<UserControls.TableItem> cmbSource;

        #endregion

        #region TableSource

        private List<Interfaces.Table> tableSource = null;
        public List<Interfaces.Table> TableSource
        {
            get
            {
                return tableSource;
            }
            set
            {
                tableSource = value;
                
                cmbSource = new ObservableCollection<UserControls.TableItem>((from tmp in value
                                                                              select (new UserControls.TableItem() { Source = tmp })).ToList()
                                                                             );
                cmbFirstTable.ItemsSource = cmbSource;
                cmbSecoundTable.ItemsSource = cmbSource;
            }
        }

        #endregion

    }

    #region TempRelationship

    public class TempRelationship: DependencyObject
    {
        #region FirstField

        public static readonly DependencyProperty FirstFieldProperty =
                               DependencyProperty.Register("FirstField", typeof(UserControls.FieldItem), typeof(TempRelationship), null);

        public UserControls.FieldItem FirstField
        {
            get { return (UserControls.FieldItem)GetValue(FirstFieldProperty); }
            set { SetValue(FirstFieldProperty, value); }
        }

        #endregion

        #region FirstFieldType

        public static readonly DependencyProperty FirstFieldTypeProperty =
                               DependencyProperty.Register("FirstFieldType", typeof(Interfaces.Field.RType), typeof(TempRelationship), null);

        public Interfaces.Field.RType FirstFieldType
        {
            get { return (Interfaces.Field.RType)GetValue(FirstFieldTypeProperty); }
            set { SetValue(FirstFieldTypeProperty, value); }
        }

        #endregion

        #region SecoundField

        public static readonly DependencyProperty SecoundFieldProperty =
                               DependencyProperty.Register("SecoundField", typeof(UserControls.FieldItem), typeof(TempRelationship), null);

        public UserControls.FieldItem SecoundField
        {
            get { return (UserControls.FieldItem)GetValue(SecoundFieldProperty); }
            set { SetValue(SecoundFieldProperty, value); }
        }

        #endregion

        #region SecoundFieldType

        public static readonly DependencyProperty SecoundFieldTypeProperty =
                               DependencyProperty.Register("SecoundFieldType", typeof(Interfaces.Field.RType), typeof(TempRelationship), null);

        public Interfaces.Field.RType SecoundFieldType
        {
            get { return (Interfaces.Field.RType)GetValue(SecoundFieldTypeProperty); }
            set { SetValue(SecoundFieldTypeProperty, value); }
        }

        #endregion
    }

    #endregion

}